package com.project.myPackage.controller;

import com.project.myPackage.common.R;
import com.project.myPackage.entity.Categories;
import com.project.myPackage.service.CategoriesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "分类管理")
@Slf4j
@RestController
@RequestMapping("/")
public class CategoriesController {
    @Autowired
    private CategoriesService categoriesService;

    @ApiOperation(value = "获取所有分类信息")
    @GetMapping("/getAllCategories")
    public R<?> getAllCategories(){
        List<Categories> list = categoriesService.list();
        if(list != null){
            return R.success(list, "获取分类列表成功！");
        }else{
            return R.error("获取分类列表失败！");
        }

    }

    @ApiOperation(value = "新增分类信息")
    @PostMapping("/addCategories")
    public R<?> addCategories(@RequestBody Categories categories){
        boolean save = categoriesService.save(categories);
        if(!save){
            return R.error("新增分类失败！");
        }
        return R.success("新增分类成功！");
    }

    @ApiOperation(value = "删除分类信息")
    @GetMapping("/deleteCategories")
    public R<?> deleteCategories(@RequestParam Integer category_id){
        boolean remove = categoriesService.removeById(category_id);
        if(!remove){
            return R.error("删除分类失败！");
        }
        return R.success("删除分类成功！");
    }

    @ApiOperation(value = "修改分类信息")
    @PostMapping("/updateCategories")
    public R<?> updateCategories(@RequestBody Categories categories){
        boolean update = categoriesService.updateById(categories);
        if(!update){
            return R.error("修改分类失败！");
        }
        return R.success("修改分类成功！");
    }

}
